package com.app.micaihu.view.comment.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;


import android.text.TextUtils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.BaseOldActivity;

import com.app.micaihu.bean.comment.NormalComment;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.custom.view.CustomImageView;

import com.app.micaihu.utils.CommentUtils1;
import com.app.micaihu.utils.ImageLoadUtils;
import com.app.micaihu.utils.InteractiveUtils;
import com.app.micaihu.view.comment.SubmitCommentListener;

import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;

import com.app.utils.util.ToastUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.listener.Clickable;
import com.app.utils.util.view.expression.view.BiaoQinTextView;
import com.blankj.utilcode.util.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Guo on 2016/12/21.
 * <p>
 * <p>
 * 次级评论的adapter,可用于评论和帖子
 */

public class SecondaryCommentAdapter extends AppBaseAdapter<NormalComment> implements View.OnClickListener, View.OnLongClickListener, Clickable.ClickListener {

    private NormalComment currentReplyEntity = null;
    private SubmitCommentListener commentListener;
    private boolean comment = true;

    public SecondaryCommentAdapter(List<NormalComment> list, Activity activity) {
        super(list, activity);

    }


    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        NormalComment normalComment = list.get(position);
        if (normalComment == null) {
            return 0;
        }
        if (!TextUtils.isEmpty(normalComment.getICommentTitle())) {
            //如果typename不为空则表示 此评论是代码加入的
            return 1;
        } else {
//            if(InteractiveUtils.getInstance().theDefriendIsContains(normalComment.getIToUid())){
//                return 3;
//            }
            return 2;
        }
    }


    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        int type = getItemViewType(position);
        if (convertView == null) {
            if (type == 2) {
                convertView = inflater.inflate(R.layout.item_news_comment, null);
                convertView.setBackgroundResource(R.drawable.item_news_commentchild_bg_selector);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else if (type == 1) {
                convertView = inflater.inflate(R.layout.layout_driver_title_margin, null);
                convertView.setBackgroundResource(R.drawable.item_news_commentchild_bg_selector);
                ((TextView) (convertView.findViewById(android.R.id.text2))).setText(list.get(position).getICommentTitle());
                return convertView;
            } else {
                convertView = inflater.inflate(R.layout.item_layout_gone, null);
                return convertView;
            }
        } else {
            if (type == 2) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else if (type == 1) {
                ((TextView) (convertView.findViewById(android.R.id.text2))).setText(list.get(position).getICommentTitle());
                return convertView;
            } else {
                return convertView;
            }
        }
        NormalComment normalComment = list.get(position);

        if (normalComment == null) {
            return convertView;
        }
        viewHolder.parentLayout.setTag(R.id.tag_first, normalComment);//设置数据标识
        viewHolder.message.setTag(R.id.tag_first, normalComment);
        viewHolder.replyLayout.setTag(normalComment);//设置数据标识
        viewHolder.readAllText.setTag(normalComment);
        viewHolder.userImg.setTag(normalComment);
        viewHolder.userName.setTag(normalComment);

//        LoadImageUtils.getInstance().loadCircleImage(viewHolder.userImg, normalComment.getHeadPic());
        LoadImageUtils.getInstance().loadListCircleImage(viewHolder.userImg, normalComment.getHeadPic());


//        LoadImageUtils.getInstance().loadImage(viewHolder.userRank, normalComment.getRankIcon());
        ImageLoadUtils.loadImage(normalComment.getRankIcon(), viewHolder.userRank);
        ImageLoadUtils.loadImage(normalComment.getHonourPic(), viewHolder.ivHonourPic);

        viewHolder.tvArmyGroupInfo.setText(normalComment.getArmyGroupInfo());
        viewHolder.userName.setText(normalComment.getNickName());
        viewHolder.rankname.setText(normalComment.getRankName());
        viewHolder.commentTime.setText(normalComment.getPublishTime());
        viewHolder.commentZan.setText(TextUtils.isEmpty(normalComment.getPraiseNum()) || normalComment.getPraiseNum().equals("0") ? "" : normalComment.getPraiseNum());
        setColorText(viewHolder.message, normalComment);

        //判断是否点赞过
        if (InteractiveUtils.getInstance().theLaudIsContains(normalComment.getICommentId(), true)) {
            viewHolder.commentZan.setEnabled(false);
            viewHolder.laudContainer.setTag(true);
        } else {
            viewHolder.commentZan.setEnabled(true);
            viewHolder.laudContainer.setTag(normalComment);
        }

        return convertView;
    }

    /**
     * 设置变颜色的字体的方法
     *
     * @param message
     * @param normalComment
     */
    private void setColorText(BiaoQinTextView message, NormalComment normalComment) {
        ArrayList<String> strs = new ArrayList<>();
        ArrayList<Integer> color = new ArrayList<>();
        ArrayList<Object> parms = new ArrayList<>();
        String type = "";

        if (TextUtils.equals(normalComment.getIType(), TypeConstant.COMMENT_TWO)) {
            type = "@";
        }
        //添加第一个参数
        if (!TextUtils.isEmpty(type)) {
            strs.add(type + normalComment.getpNickName() + " ");
            color.add(Color.parseColor("#546a93"));
            parms.add(normalComment.getpUid());
        }
        //添加第二个参数
        strs.add(normalComment.getIContent());
        //日间颜色
        color.add(Color.parseColor("#ff505050"));
        parms.add(normalComment);
//        message.setText(strs,color,parms,this);
        message.setTextNoMax(strs, color, parms, this);

    }


    @Override
    public boolean onLongClick(View v) {
        if (v == null) return false;
        switch (v.getId()) {
            case R.id.comment_parentlayout:
            case R.id.comment_message:
                if (v.getTag(R.id.tag_first) instanceof NormalComment && v.getTag(R.id.tag_first) != null) {
                    this.currentReplyEntity = (NormalComment) v.getTag(R.id.tag_first);
                    InteractiveUtils.getInstance().showCommentPop(v, this);

                }
                return true;
        }
        return false;
    }


    /*************** 事件 start *************/
    @Override
    public void onClick(View view) {
        if (view == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.fl_laud_container://点赞
                InteractiveUtils.getInstance().laud((FrameLayout) view, null);
                break;
            case R.id.comment_parentlayout://点击父布局弹出评论框
                if (view.getTag(R.id.tag_first) instanceof NormalComment && view.getTag(R.id.tag_first) != null) {
                    this.currentReplyEntity = (NormalComment) view.getTag(R.id.tag_first);
                    replyComment();
                }
                break;
            case R.id.reply://菜单回复
                InteractiveUtils.getInstance().dimissCommentPop();
                replyComment();
                break;
            case R.id.jubao://菜单举报
                InteractiveUtils.getInstance().dimissCommentPop();
                InteractiveUtils.getInstance().report(view, currentReplyEntity, comment);
                break;
            case R.id.fuzhi://菜单复制
                InteractiveUtils.getInstance().dimissCommentPop();
                InteractiveUtils.getInstance().copy(currentReplyEntity);
                break;
            case R.id.delete://删除评论
                InteractiveUtils.getInstance().dimissCommentPop();
                deleteComment();
                break;
            case R.id.comment_username://点击名字
            case R.id.comment_usericon://点击头像
                if (view.getTag() != null & view.getTag() instanceof NormalComment) {
                    this.currentReplyEntity = (NormalComment) view.getTag();

                    MyHomepageDynamicActivity.goToUserHomePage((Activity) context, currentReplyEntity.getIToUid());
                }

                break;
        }
    }


    /**
     * 删除评论
     */
    private void deleteComment() {
        SubmitCommentListener deleteComtListener = new SubmitCommentListener() {
            @Override
            public void startSubmit() {
            }

            @Override
            public void errorSubmit(VolleyError error) {
                ToastUtils.makeText(AppApplication.getInstance().getString(R.string.common_delete_failure));
            }

            @Override
            public void successSubmit(NormalComment normalComment) {
                if (currentReplyEntity == null) {
                    return;
                }

                list.remove(currentReplyEntity);
                notifyDataSetChanged();
            }
        };
        CommentUtils1.getInstance().showDeleteDialog((Activity) context, currentReplyEntity, deleteComtListener, !comment);


    }


    /**
     * 回复当前item--弹出评论框
     */
    private void replyComment() {

        if (this.currentReplyEntity == null) {
            return;
        }
        if (commentListener == null) {
            commentListener = new SubmitCommentListener() {
                @Override
                public void startSubmit() {

                }

                @Override
                public void errorSubmit(VolleyError error) {

                }

                @Override
                public void successSubmit(NormalComment normalComment) {

                    if (normalComment == null || list == null) {
                        return;
                    }
                    if (list.size() == 0) {
                        //在第一个位置添加最新评论
                        NormalComment comment = new NormalComment();
                        comment.setCommentTitle(AppApplication.getInstance().getString(R.string.speak_title_all));
                        list.add(0, comment);
                    }
                    list.add(1, normalComment);
                    notifyDataSetChanged();
                }
            };
        }

        CommentUtils1.getInstance().showDialog((Activity) context, null,
                currentReplyEntity.getNewsId(), currentReplyEntity, commentListener,
                currentReplyEntity.getNewsOrTopic(), false);
    }


    /**
     * SpanableString 的点击事件
     *
     * @param position
     * @param parms
     */
    @Override
    public void click(int position, ArrayList<Object> parms) {
        if (parms == null) {
            return;
        }
        if (parms.size() == 1) {

            try {
                currentReplyEntity = (NormalComment) parms.get(0);
            } catch (Exception e) {
                currentReplyEntity = null;
            }
            replyComment();
        } else if (parms.size() == 2) {
            if (position == 0) {
                String uid = parms.get(0).toString();
                //点击评论用户昵称
                MyHomepageDynamicActivity.goToUserHomePage(ActivityUtils.getTopActivity(), uid);
            } else if (position == 1) {
                try {
                    currentReplyEntity = (NormalComment) parms.get(1);
                } catch (Exception e) {
                    currentReplyEntity = null;
                }
                replyComment();
            }
        }
    }


    class ViewHolder {

        CustomImageView userImg;//头像
        CustomImageView userRank;//军衔
        ImageView ivHonourPic;//粉絲標志
        TextView userName;//名称
        TextView tvArmyGroupInfo;//名称
        TextView rankname;//军衔名称
        TextView commentZan;//点赞
        TextView commentTime;//发布时间
        BiaoQinTextView message;//评论内容
        LinearLayout replyLayout;//评论回复模块
        TextView readAllText;//查看全文
        View parentLayout;//父布局
        View lineview;//下划线
        FrameLayout laudContainer;

        public ViewHolder(View convertView) {
            parentLayout = convertView.findViewById(R.id.comment_parentlayout);
            userImg = (CustomImageView) convertView.findViewById(R.id.comment_usericon);
            userName = (TextView) convertView.findViewById(R.id.comment_username);
            userRank = (CustomImageView) convertView.findViewById(R.id.comment_rank);
            ivHonourPic = convertView.findViewById(R.id.ivHonourPic);
            tvArmyGroupInfo = convertView.findViewById(R.id.tvArmyGroupInfo);
            rankname = (TextView) convertView.findViewById(R.id.comment_rank_name);
            commentTime = (TextView) convertView.findViewById(R.id.comment_time);
            commentZan = (TextView) convertView.findViewById(R.id.comment_zan);
            laudContainer = (FrameLayout) convertView.findViewById(R.id.fl_laud_container);
            message = (BiaoQinTextView) convertView.findViewById(R.id.comment_message);

            message.setMaxLines(Integer.MAX_VALUE);

            replyLayout = (LinearLayout) convertView.findViewById(R.id.comment_reply_layout);

            readAllText = (TextView) convertView.findViewById(R.id.comment_readall);
            readAllText.setVisibility(View.GONE);


            lineview = convertView.findViewById(R.id.item_divider);
            replyLayout.setOnClickListener(SecondaryCommentAdapter.this);
            parentLayout.setOnClickListener(SecondaryCommentAdapter.this);

            laudContainer.setOnClickListener(SecondaryCommentAdapter.this);
            readAllText.setOnClickListener(SecondaryCommentAdapter.this);
            userImg.setOnClickListener(SecondaryCommentAdapter.this);
            userName.setOnClickListener(SecondaryCommentAdapter.this);
            message.setOnClickListener(SecondaryCommentAdapter.this);


            parentLayout.setOnLongClickListener(SecondaryCommentAdapter.this);
            message.setOnLongClickListener(SecondaryCommentAdapter.this);


        }

    }

}
